﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;

namespace NeoFramework.Net
{
    public class Page
    {
        public String HTML = "";
        public String title = "";
        public String[] links;

        public Hashtable requestHeaders = new Hashtable();
        public Hashtable responseHeaders = new Hashtable();

        public DateTime timeRequested;

        public HTTPWrapper wrapper = new HTTPWrapper();

        private static Hashtable listeners = new Hashtable();

        private String URL;
        private CookieJar cookies;
        private Hashtable postData;
        private Hashtable customHeaders;

        public Page(String URL)
        {
            this.URL = URL;
            this.cookies = new CookieJar();
            this.postData = new Hashtable();
            this.customHeaders = new Hashtable();
        }

        public Page(String URL, CookieJar cookies)
        {
            this.URL = URL;
            this.cookies = cookies;
            this.postData = new Hashtable();
            this.customHeaders = new Hashtable();
        }

        public Page(String URL, Hashtable customHeaders)
        {
            this.URL = URL;
            this.cookies = new CookieJar();
            this.postData = new Hashtable();
            this.customHeaders = customHeaders;
        }

        public Page(String URL, Hashtable postData, Hashtable customHeaders)
        {
            this.URL = URL;
            this.cookies = new CookieJar();
            this.postData = postData;
            this.customHeaders = customHeaders;
        }

        public Page(String URL, CookieJar cookies, Hashtable postData)
        {
            this.URL = URL;
            this.cookies = cookies;
            this.postData = postData;
            this.customHeaders = new Hashtable();
        }

        public Page(String URL, CookieJar cookies, Hashtable customHeaders, Hashtable postData)
        {
            this.URL = URL;
            this.cookies = cookies;
            this.postData = postData;
            this.customHeaders = customHeaders;
        }

        public String sendRequest()
        {
            this.wrapper.cJar = this.cookies;
            HTTPWrapper.Type RType = this.postData.Count > 0 ? HTTPWrapper.Type.POST : HTTPWrapper.Type.GET;

            this.timeRequested = DateTime.Now;

            this.HTML = this.wrapper.request(RType, this.URL, this.postData, this.customHeaders);

            if (HTML.IndexOf("<title>") > 0)
            {
                Regex titleReg = new Regex("<title>(.*?)<\\/title>", RegexOptions.IgnoreCase);
                Match m = titleReg.Match(HTML);
                this.title = m.Groups[1].ToString();
            }

            String urlPat = "<<a\\s+([^>]*)href=\"https?:\\/\\/([^\"]*)\"(.*?)>(.*?)<\\/a>";
            Regex regUrl = new Regex(urlPat, RegexOptions.IgnoreCase);
            ICollection<String> urlList = new List<String>();
            Match mat = regUrl.Match(HTML);

            while (mat.Success)
            {
                urlList.Add(mat.Groups[0].ToString());
                mat = mat.NextMatch();
            }

            this.links = urlList.ToArray<String>();
            this.requestHeaders = this.wrapper.headers;
            this.responseHeaders = this.wrapper.returnHeaders;

            foreach (PageListener listen in Page.listeners.Values)
            {
                listen.pageRecieved(this);
            }

            return this.HTML;
        }

        public CookieJar getCookieJar()
        {
            return this.wrapper.cJar;
        }

        public static void addPageListener(String name, PageListener listener)
        {
            Page.listeners.Add(name, listener);
        }

        public static void removePageListener(String name)
        {
            Page.listeners.Remove(name);
        }
    }
}
